package com.splant.android.smart.UILevel.Community;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.loopj.android.http.RequestParams;
import com.splant.android.R;
import com.splant.android.smart.CommunicationLevel.sPlantAsyncHttpClient;
import com.splant.android.smart.CommunicationLevel.sPlantJsonHttpResponseHandler;
import com.splant.android.smart.ModelLevel.WaterRecordListBean;
import com.splant.android.smart.SystemDefinedLevel.GenericControls.DateTimeSelceted.DateTimeSet;
import com.splant.android.smart.SystemDefinedLevel.GenericControls.DateTimeSelceted.SlideDateTimeListener;
import com.splant.android.smart.SystemDefinedLevel.GenericControls.DateTimeSelceted.SlideDateTimePicker;
import com.splant.android.smart.SystemDefinedLevel.Utils.System_Util;
import org.apache.http.Header;
import org.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class Garden_WaterSwitchRecord_Activity extends FragmentActivity implements View.OnClickListener {


    private Button btn_goBack;
    private int switchNo;
    private String waterId = "";
    private String startTime = "";
    private String endTime = "";
    private TextView tv_waterDatas;
    private Button btn_query;
    private TextView tv_startTime;
    private TextView tv_endTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.garden_waterswitchrecord_activity);

        btn_goBack = (Button) findViewById(R.id.btn_goBack);
        tv_startTime = (TextView) findViewById(R.id.tv_startTime);
        tv_endTime = (TextView) findViewById(R.id.tv_endTime);

        tv_waterDatas = (TextView) findViewById(R.id.tv_waterDatas);
        btn_query = (Button) findViewById(R.id.btn_query);

        switchNo = getIntent().getIntExtra("switchNo", 1);
        waterId = getIntent().getStringExtra("waterId");

        init();

        btn_goBack.setOnClickListener(this);
        btn_query.setOnClickListener(this);
        tv_startTime.setOnClickListener(this);
        tv_endTime.setOnClickListener(this);
    }

    private sPlantAsyncHttpClient sPlantClient;

    private void init() {
        sPlantClient = sPlantAsyncHttpClient.getInstance();

        startTime = System_Util.getSystemCurrentTime("yyyy-MM-dd");
        endTime = startTime;

        tv_startTime.setText(startTime);
        tv_endTime.setText(endTime);

        getDatas();
    }

    private void getDatas() {
        RequestParams params = new RequestParams();
        params.put("waterId", waterId);
        params.put("startTime", startTime + " 00:00:00");
        params.put("endTime", endTime + " 23:59:59");
        params.put("switchNo", switchNo);

        sPlantClient.get(sPlantAsyncHttpClient.GETWATERDATAS, params, new sPlantJsonHttpResponseHandler() {

            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                try {
                    if (response.getInt("succeed") == 1) {

                        Gson gson = new Gson();

                        WaterRecordListBean waterRecordListBean = gson.fromJson(response.toString(), WaterRecordListBean.class);

                        showWaterDatas(waterRecordListBean.getWaterDataList());

                    }
                } catch (Exception e) {

                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {

                Toast.makeText(Garden_WaterSwitchRecord_Activity.this, getString(R.string.system_request_error), Toast.LENGTH_SHORT).show();

            }
        });

    }

    private void showWaterDatas(List<WaterRecordListBean.WaterData> waterDatas) {
        String datas = "";
        if (waterDatas != null) {
            for (WaterRecordListBean.WaterData wd : waterDatas) {
                datas += wd.getWaterTime() + " " + wd.getWaterLength() + " " + wd.getUserName() + "\n";
            }
            tv_waterDatas.setText(datas);
        }
    }

    private boolean selectDateTime = false;

    private void selectDate() {
        try {
            String date = "";
            if (selectDateTime) {
                date = tv_startTime.getText().toString();
            } else {
                date = tv_endTime.getText().toString();
            }
            Date birthdayDate = sdf.parse(date);
            DateTimeSet.dateTimeType = DateTimeSet.DateTimeType.DATE;
            new SlideDateTimePicker.Builder(getSupportFragmentManager())
                    .setListener(slideDateTimeListener)
                    .setInitialDate(birthdayDate)
                    //.setMinDate(minDate)
                    //.setMaxDate(maxDate)
                    //.setIs24HourTime(true)
                    .setTheme(SlideDateTimePicker.HOLO_LIGHT)
                    // .setIndicatorColor(R.color.system_appBg)
                    .build()
                    .show();

        } catch (Exception e) {

        }
    }

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    private SlideDateTimeListener slideDateTimeListener = new SlideDateTimeListener() {

        @Override
        public void onDateTimeSet(Date date) {

            if (selectDateTime) {
                startTime = sdf.format(date);
                tv_startTime.setText(startTime);
            } else {
                endTime = sdf.format(date);
                tv_endTime.setText(endTime);
            }

        }

        @Override
        public void onDateTimeCancel() {

        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_goBack:
                finish();
                break;
            case R.id.btn_query:
                getDatas();
                break;
            case R.id.tv_startTime:
                selectDateTime = true;
                selectDate();
                break;
            case R.id.tv_endTime:
                selectDateTime = false;
                selectDate();
                break;
        }
    }

}
